package com.zhangxiaofanfan.hotcode.third;

/**
 * TODO
 *
 * @author zhangxiaofanfan
 * @date 2024-05-30 08:21:47
 */
public class HotCode039 {
    public static void main(String[] args) {

    }

    private boolean symmetric = true;
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        isSymmetric(root.left, root.right);
        return symmetric;
    }

    public boolean isSymmetric(TreeNode left, TreeNode right) {
        if (!symmetric) {
            return false;
        }
        if (left == null && right == null) {
            return true;
        }
        if (left == null || right == null || left.val != right.val) {
            symmetric = false;
            return false;
        }
        symmetric = isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
        return symmetric;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
